package com.future.mayi.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.mayi.R;
import com.future.mayi.dialog.SkuEdtNumDialog;
import com.future.mayi.model.GoodsBatchModel;
import com.future.mayi.model.SpecModel;
import com.future.mayi.model.interfaces.GoodsBatchItemListener;
import com.future.mayi.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoodsBatchItemView extends LinearLayout implements View.OnClickListener {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    @BindView(R.id.tv_goods_hint)
    TextView tvGoodsHint;
    @BindView(R.id.fl_goods)
    FlowLayout flGoods;
    @BindView(R.id.edt_address)
    EditText edtAddress;
    @BindView(R.id.edt_note)
    EditText edtNote;


    private GoodsBatchModel model;
    private int position;

    private LayoutInflater mInflater;

    private GoodsBatchItemListener listener;

    private Context mContext;

    public GoodsBatchItemView(Context context) {
        this(context, null);
    }

    public GoodsBatchItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GoodsBatchItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        View.inflate(context, R.layout.layout_goods_batch_item, this);
        ButterKnife.bind(this);
        mInflater = LayoutInflater.from(context);
        edtAddress.addTextChangedListener(textWatcher);
        edtNote.addTextChangedListener(noteTextWatcher);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (model != null) {
                model.setAddress(edtAddress.getText().toString());
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };


    private TextWatcher noteTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (model != null) {
                model.setMemo(edtNote.getText().toString());
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    private SkuEdtNumDialog skuEdtNumDialog;

    @OnClick({R.id.tv_delete, R.id.rl_goods})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_delete:
                if (listener != null) listener.deleteItem(this);
                break;
            case R.id.rl_goods:
                if (listener != null) listener.showAddGoods(this);
                break;
            case R.id.ll_sku_item:
                if (skuEdtNumDialog == null)
                    skuEdtNumDialog = new SkuEdtNumDialog(mContext);
                skuEdtNumDialog.show(view);
                break;
            case R.id.iv_delete:
                if (model == null) return;
                if (model.getSelectList() == null) return;
                if (model.getList() == null) return;
                View parent = (View) view.getTag();
                SpecModel specModel = (SpecModel) parent.getTag();
                model.getList().add(specModel);
                model.getSelectList().remove(specModel);
                flGoods.removeView(parent);
                break;
        }
    }

    public void setListener(GoodsBatchItemListener listener) {
        this.listener = listener;
    }


    public List<SpecModel> getList() {
        return model == null ? null : model.getList();
    }

    public List<SpecModel> getSelectList() {
        return model == null ? null : model.getSelectList();
    }

    public void setModel(GoodsBatchModel model, int position) {
        this.position = position;
        tvTitle.setText("订单" + (position + 1));
        if (this.model == model) return;
        this.model = model;
        edtAddress.removeTextChangedListener(textWatcher);
        edtNote.removeTextChangedListener(noteTextWatcher);
        edtAddress.setText(model.getAddress() == null ? "" : model.getAddress());
        if (!TextUtils.isEmpty(model.getAddress()))
            edtAddress.setSelection(model.getAddress().length());

        edtNote.setText(model.getMemo() == null ? "" : model.getMemo());
        if (!TextUtils.isEmpty(model.getMemo()))
            edtNote.setSelection(model.getMemo().length());

        flGoods.removeAllViews();
        if (model.getSelectList() != null && model.getSelectList().size() > 0) {
            for (SpecModel specModel : model.getSelectList()) {
                View view = mInflater.inflate(R.layout.layout_batch_goods_item_view, flGoods, false);
                TextView tvName = view.findViewById(R.id.tv_name);
                View viewDelete = view.findViewById(R.id.iv_delete);
                tvName.setText(specModel.getGoods_spec() + (" *" + specModel.getNum()));
                view.setTag(specModel);
                view.setOnClickListener(this);
                viewDelete.setTag(view);
                viewDelete.setOnClickListener(this);
                flGoods.addView(view);
            }
        }
        edtAddress.addTextChangedListener(textWatcher);
        edtNote.addTextChangedListener(noteTextWatcher);
    }

    public void addItem(SpecModel specModel) {
        if (getList() == null || getList().size() == 0) return;
        getList().remove(specModel);
        specModel.setNum(1);
        if (getSelectList() == null) model.setSelectList(new ArrayList<SpecModel>());
        getSelectList().add(specModel);
        View view = mInflater.inflate(R.layout.layout_batch_goods_item_view, flGoods, false);
        TextView tvName = view.findViewById(R.id.tv_name);
        View viewDelete = view.findViewById(R.id.iv_delete);
        tvName.setText(specModel.getGoods_spec() + (" *" + specModel.getNum()));
        view.setTag(specModel);
        view.setOnClickListener(this);
        viewDelete.setTag(view);
        viewDelete.setOnClickListener(this);
        flGoods.addView(view);
    }

    public int getPosition() {
        return position;
    }


}
